package com.springbootblog.service.serviceImpl.fontend;

import com.springbootblog.controller.fontend.IndexController;
import com.springbootblog.dao.FooterDao;
import com.springbootblog.pojo.Friendlink;
import com.springbootblog.service.fontend.FooterService;
import com.springbootblog.utils.Function;
import com.springbootblog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FooterServiceImpl implements FooterService
{
    /**
     * 定义一个静态常量日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    /**
     * 自动装载（dao接口注入）
     */
    @Autowired
    private FooterDao footerDao;

    /**
     * 获取底部数据
     * @param request
     * @return
     */
    @Override
    public Map<String, Object> getFooterData(HttpServletRequest request)
    {
        // 查访客总数
        int visit = (int)footerDao.getTotalBrowse();
        // 查文章总数
        int article = (int)footerDao.getTotalArticle();
        // 查用户总数
        int user = (int)footerDao.getTotalUser();
        // 留言总数
        int message = (int)footerDao.getTotalMessage();
        // 友情链接列表
        List<Friendlink> friendLinklist = footerDao.getFriendLinkList();

        // 运行天数
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse("2018-07-21", formatter);
        LocalDate endDate = LocalDate.parse(Function.getCurrentFormat("yyyy-MM-dd"), formatter);
        int days = (int)Function.getDaysBetween(startDate,endDate);

        /*//获取session对象
        HttpSession session = request.getSession();
        Object userid = session.getAttribute("id");
        Object figureurl = session.getAttribute("figureurl");
        Object nickname = session.getAttribute("nickname");
        Object email = session.getAttribute("email");//*/

        // 获取token
        String token = request.getHeader(JwtUtils.token_name);
        Object userid = "";
        Object figureurl = "";
        Object nickname = "";
        Object email = "";
        if(token == "null" || token == null || token.equals("") || token.equals("undefined") )
        {}
        else
        {
            // 解析token
            Claims paramList = JwtUtils.parseJWT(token);
            if(paramList == null || paramList.equals(null))
            {}
            else
            {
                userid = paramList.get("id");
                figureurl = paramList.get("figureurl");
                nickname = paramList.get("nickname");
                email = paramList.get("email");
            }

        }

        Map<String,Object> result = new HashMap<>(12);
        // result.put("token",token);
        result.put("code",200);
        result.put("msg","操作成功！");
        result.put("visit",visit);
        result.put("friendLinklist",friendLinklist);
        result.put("article",article);
        result.put("user",user);
        result.put("message",message);
        result.put("userid",userid);
        result.put("figureurl",figureurl == null ? "" : figureurl);
        result.put("nickname",nickname);
        result.put("email",email);
        result.put("days",days);
        return result;
    }
}
